<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Index</title>
    <!-- Required Stylesheets -->
    <link href="/layui/css/layui.css" rel="stylesheet" />
</head>
<body>
<div class="layui-btn-container common">
    <button type="button" class="layui-btn" id="btnAdd">增加</button>
    <button type="button" class="layui-btn" id="btnEdit">编辑</button>
    <button type="button" class="layui-btn" id="btnDel">删除</button>
</div>
<div class="container">
    <div class="layui-col-md12">
        <table class="layui-hide" id="warehouseGrid" lay-filter="warehouseGrid"></table>
    </div>
</div>
<!-- Required Javascript -->
<script src="/js/jquery-3.3.1.min.js"></script>
<script src="/layui/layui.all.js"></script>
<script src="/layui/extend/tableSelect.js"></script>

<script>
    var table = layui.table;
    table.render({
        elem: '#warehouseGrid'
        , url: '/warehouse/getListWithPage'
        , cellMinWidth: 80 //全局定义常规单元格的最小宽度，layui 2.2.1 新增
        , cols: [[
            { checkbox: true }
            , { field: 'iID', width: 80, title: 'ID', sort: true }
            , { field: 'cWhCode', width: 150, title: '仓库编码', sort: true }
            , { field: 'cWhName', width: 150, title: '仓库名称', sort: true }
            , { field: 'cWhAddress', width: 150, title: '地址', sort: true }
        ]]
        , id: 'warehouseGrid'
        , page: true
        , limit: 10
        // , method: 'post'  //提交方式
        , toolbar: '#toolbarDemo' //开启头部工具栏，并为其绑定左侧模板
        , defaultToolbar: ['filter', 'exports', 'print'] //自定义头部工具栏右侧图标。如无需自定义，去除该参数即可
    });
</script>
<script>
    //弹出一个iframe层
    $('#btnAdd').on('click', function () {
        layer.open({
            type: 2,
            id: "WarehouseCreate",
            title: '新增仓库',
            maxmin: true,
            shadeClose: true, //点击遮罩关闭层
            area: ['800px', '520px'],
            content: '/warehouse/create',
            end: function () {
                table.reload('warehouseGrid', {
                });
            }
        });

    });

    $('#btnEdit').on('click', function () {
        var checkStatus = table.checkStatus('warehouseGrid')
            , data = checkStatus.data;
        if (data.length == 0) {
            //配置一个透明的询问框
            layer.msg('请先选择要修改的仓库。', {
                time: 5000, //20s后自动关闭
                btn: ['确认']
            });
            return;
        }

        if (data.length > 1) {
            //配置一个透明的询问框
            layer.msg('请只选择一个要修改的仓库。', {
                time: 5000, //20s后自动关闭
                btn: ['确认']
            });
            return;
        }

        layer.open({
            type: 2,
            id: "WarehouseEdit",
            title: '修改仓库',
            maxmin: true,
            shadeClose: true, //点击遮罩关闭层
            area: ['800px', '520px'],
            content: '/warehouse/update?id=' + data[0].iID,
            end: function () {
                table.reload('warehouseGrid', {
                });
            }
        });
    });

    $('#btnDel').on('click', function () {
        var checkStatus = table.checkStatus('warehouseGrid')
            , data = checkStatus.data;
        //console.log(data[0].iID);
        //var jsonObj = JSON.parse(data);//转换为json对象
        //console.log(jsonObj);
        if (data.length = 0) {
            //配置一个透明的询问框
            layer.msg('请先选择要删除的仓库。', {
                time: 5000, //20s后自动关闭
                btn: ['确认']
            });
            return;
        }

        if (data.length > 1) {
            //配置一个透明的询问框
            layer.msg('请只选择一个要删除的仓库。', {
                time: 5000, //20s后自动关闭
                btn: ['确认']
            });
            return;
        }

        layer.confirm('是否要删除当前所选择的仓库？', {
            btn: ['确认', '取消'] //可以无限个按钮
        }, function (index, layero) {
            var checkStatus = table.checkStatus('warehouseGrid')
                , data = checkStatus.data;
            var ids = [];
            for (var i = 0; i < data.length;i++) {
                ids.push(data[i].iID);
            }

            //按钮【确认】的回调
            $.ajax({
                type: "POST",
                data: JSON.stringify(ids),//"id=" + ids.toString(),
                dataType: "json",
                contentType : 'application/json;charset=utf-8',
                async: false,
                url: "/warehouse/deleteList",
                success: function (msg) {
                    if (msg.code == "0") {
                        table.reload('warehouseGrid', {
                            //url: '/api/table/search'
                            //, where: {} //设定异步数据接口的额外参数
                            //,height: 300
                        });
                    }
                    else {
                        layer.msg('错误号：' + msg.code + ' 错误信息：' + msg.msg, { icon: 2 });
                    }
                }
            });
            layer.close(index);
        }, function (index, layro) {
            //按钮【取消】的回调
        });
    });
</script>
</body>
</html>
